#include "alertinfo.h"
#include "ui_alertinfo.h"

AlertInfo::AlertInfo(){
}

AlertInfo::AlertInfo(Alert alert){
    int index = 0;
    QString buffer;
    setupUi(this);
    this->alert = alert;
    this->id = alert.getid();
    this->name = alert.getname();
    this->info = alert.getinfo();
    this->datetime = alert.getdatetime();
    this-> nameID = alert.getnameid();
    this->setWindowTitle(QString::fromStdString(alert.getname()));

    if(this->alert.getrecurring()){
        buffer = "Yes";
    }
    else
        buffer = "No";
    index = alertInfoRecurring->findText(buffer);

    if(index != -1){
        alertInfoRecurring->setCurrentIndex(index);
    }

    if(buffer == "Yes"){
        alertInfoFrequency->setDisabled(false);
        index = alertInfoFrequency->findText(QString::fromStdString(this->alert.getfrequency()));
        if(index != -1){
            alertInfoFrequency->setCurrentIndex(index);
        }
    }
    else
        alertInfoFrequency->setDisabled(true);

    if(alert.getdisabled())
        alertInfoDisabled->setChecked(true);

    this->alertInfoName->setText(QString::fromStdString(alert.getname()));
    this->alertInfoDateTime->setDateTime(alert.getdatetime());
    this->alertInfoInfo->setText(QString::fromStdString(alert.getinfo()));

    setWindowFlags(Qt::WindowTitleHint | Qt::WindowMinimizeButtonHint);
    connect(this, SIGNAL(on_alertAcceptButton_clicked()), this, SLOT(checkEdited()));
    connect(this, SIGNAL(on_deleteAlertButton_clicked()), this, SLOT(removeAlert()));
    connect(alertInfoFrequency, SIGNAL(currentIndexChanged(QString)), this, SLOT(setFrequency(QString)));
    connect(alertInfoRecurring, SIGNAL(currentIndexChanged(QString)), this, SLOT(enableFrequency(QString)));
    connect(alertInfoDisabled, SIGNAL(toggled(bool)), this, SLOT(setDisabled(bool)));
    layout() -> setSizeConstraint(QLayout::SetFixedSize);
}

void AlertInfo::removeAlert(){
    remove(this->alert);
    this->close();
}
void AlertInfo::checkEdited(){
    newAlert.setname(alertInfoName->text().toStdString());
    newAlert.setid(this->alert.getid());
    newAlert.setinfo(alertInfoInfo->toPlainText().toStdString());
    newAlert.setdatetime(alertInfoDateTime->dateTime());
    newAlert.setnameid(this->alert.getid().append(" ").append(alertInfoName->text().toStdString()));
    newAlert.setactivated(false);
    newAlert.print();
    modify(this->alert, newAlert);
    this->close();
}

void AlertInfo::enableFrequency(QString input){
    if(input == "Yes"){
        alertInfoFrequency->setDisabled(false);
        newAlert.setrecurring(true);
        newAlert.setfrequency(alertInfoFrequency->currentText().toStdString());
    }
    if(input == "No"){
        alertInfoFrequency->setDisabled(true);
        newAlert.setrecurring(false);
    }
}

void AlertInfo::setFrequency(QString input){
    newAlert.setfrequency(input.toStdString());
}

void AlertInfo::setDisabled(bool input){
    newAlert.setdisabled(input);
}
